<?php
namespace Admin\Controller;

class ClassController extends BaseController {

    public function index() {

        $profession_id = $_REQUEST['pid'];
        $map = null;
        $Profession = D ('Profession');
        $pids = $Profession->getAllPidByDid($this->admin['department_id']);

        // 构建条件
        if (in_array($profession_id,$pids)) {
            $map['profession_id'] = $profession_id;
        } else {
            $map['profession_id'] = array('in',$pids);
        }

        $Class = D ('Class');

        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Class->getCountByWhere($map),10);

        //P($limit);

        $professionList = $Profession->getAllProfessionByDid($this->admin['department_id']);

        $classList = $Class->getClassByWhere($map,$limit,true);

        // 获取系别名称
        foreach ($classList as $k=>$v) {
            $classList[$k]['pname'] = $Profession->getProfessionName($v['profession_id']);
        }

        $this->assign('classList',$classList);
        $this->assign('professionList',$professionList);
        $this->assign('page',$page);
        $this->assign('pid',$profession_id);
        $this->display();
    }



    public function add () {

        if (!IS_POST) {
            $Profession = D ('Profession');
            $professionList = $Profession->getAllProfessionByDid($this->admin['department_id']);

            $this->assign('professionList',$professionList);
            $this->display();
        } else {

            $data['name'] = I ('request.name','');
            $data['profession_id'] = I ('request.profession_id');
            $data['department_id'] = $this->admin['department_id'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $data['description'] = I ('request.description','');

            $Class = D ('Class');
            $flag = $Class->_add($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function edit() {
        if (!IS_POST) {
            $id = I ('request.id');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Class = D ('Class');
            $c = $Class->_get($id);
            if (empty($c)) $this->error('班级不存在');
            $this->verifyDepartmentByClass($c['profession_id']);

            $Profession = D ('Profession');
            $professionList = $Profession->getAllProfessionByDid($this->admin['department_id']);

            $this->assign('professionList',$professionList);
            $this->assign('class',$c);
            $this->display();
        } else {

            $data['id'] = I ('request.id','');
            $data['name'] = I ('request.name');
            $data['profession_id'] = I ('request.profession_id');

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $data['description'] = I ('request.description');

            $Class = D ('Class');
            $flag = $Class->_edit($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }


    public function del() {
        if (!IS_POST) $this->error('非法访问');

        $id = I ('request.id');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Class = D ('Class');
        $flag = $Class->_del($id);

        if ($flag === '000000')
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }

    /**
     * 验证当前班级是否能够被当前管理员操作
     *
     * @param $pid
     */
    protected function verifyDepartmentByClass($pid) {
        $Profession = D ('Profession');
        $did = $Profession->getDepartmentId($pid);

        if ($did !== $this->admin['department_id']) {
            $this->error('非法访问');
            die();
        }
    }

}